<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Redirect
 * @description : Redirect to a post by given post id
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Redirect extends MY_Controller {

  public function index() {

    // Getting uid
    $post_id = (string) strtoupper($this->input->get('post_id', TRUE));

    // Extracting key datas
    $type_key = (string) strtoupper(preg_replace("/[^A-Za-z]/", "", $post_id));
    $type_id = (int) intval(preg_replace("/[^0-9]/", "", $post_id));

    // Checking uid
    if (!empty($post_id) && !empty($type_key) && !empty($type_id)) {

      // Redirect to tweet using tweet id
      if ($type_key == 'T') {
        redirect('tweet/' . $type_id . '#' . $post_id);
        return;
      }

      // Redirect to tweet using reply id
      if ($type_key == 'R') {
        // Getting tweet id using tweet reply
        $tweet_id = 0;
        $get_reply_by_id = $this->tweet_model->get_reply_by_id($type_id);
        if ($get_reply_by_id['status'] == 1 && !empty($get_reply_by_id['result'])) {
          $tweet_id = $get_reply_by_id['result']['tweet_reply_tweet_id'];
        }
        redirect('tweet/' . $tweet_id . '?limit=999' . '#' . $post_id);
        return;
      }

      // Redirecting to poll
      if ($type_key == 'P') {
        // Checking weather completed poll 
        $completed_poll = FALSE;
        $get_poll_by_id = $this->poll_model->get_poll_by_id($type_id, NULL);
        if ($get_poll_by_id['status'] == 1 && !empty($get_poll_by_id['result'])) {
          if ($get_poll_by_id['result']['poll_status'] == 2) {
            $completed_poll = TRUE;
          }
        }
        if ($completed_poll) {
          redirect('polls/view_completed/' . $type_id);
        } else {
          redirect('polls/view/' . $type_id);
        }
        return;
      }

      // Redirecting to album
      if ($type_key == 'A') {
        redirect('album/' . $type_id);
        return;
      }

      // Redirecting to photo
      if ($type_key == 'PO') {
        redirect('album/photo/' . $type_id);
        return;
      }

      // Redirecting to video
      if ($type_key == 'VO') {
        redirect('album/video/' . $type_id);
        return;
      }

      // Redirecting to comment page
      if ($type_key == 'C') {

        // Getting comment details
        $comment_type = NULL;
        $comment_type_id = NULL;
        $get_comment_by_id = $this->comment_model->get_comment_by_id($type_id);
        if ($get_comment_by_id['status'] == 1 && !empty($get_comment_by_id['result'])) {
          $comment_type = $get_comment_by_id['result']['comment_type'];
          $comment_type_id = $get_comment_by_id['result']['comment_type_id'];
        }

        // If commented on poll
        if ($comment_type == 'poll') {
          $completed_poll = FALSE;
          $get_poll_by_id = $this->poll_model->get_poll_by_id($comment_type_id, NULL);
          if ($get_poll_by_id['status'] == 1 && !empty($get_poll_by_id['result'])) {
            if ($get_poll_by_id['result']['poll_status'] == 2) {
              $completed_poll = TRUE;
            }
          }
          if ($completed_poll) {
            redirect('polls/view_completed/' . $comment_type_id . '?limit=999' . '#' . $post_id);
          } else {
            redirect('polls/view/' . $comment_type_id . '?limit=999' . '#' . $post_id);
          }
          return;
        }

        // If commented on photo
        if ($comment_type == 'photo') {
          redirect('album/photo/' . $comment_type_id . '?limit=999' . '#' . $post_id);
        }

        // If commented on video
        if ($comment_type == 'video') {
          redirect('album/video/' . $comment_type_id . '?limit=999' . '#' . $post_id);
        }
      }

      // Redirecting to video
      if ($type_key == 'W') {
        $get_wall_post_by_id = $this->wall_model->get_wall_post_by_id($type_id);
        if (!empty($get_wall_post_by_id['result'])) {
          redirect('wall/' . $get_wall_post_by_id['result']['user_name'] . '#' . $post_id);
        }
      }
    }

    redirect();
  }

}

?>